﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskManager
{
    public partial class SettingsForm_TaskManager : Form
    {
        SettingData default_settings = new SettingData();   // 默认设置
        SettingData settings = new SettingData();           // 当前设置信息
        bool loading = false;

        public SettingsForm_TaskManager()
        {
            InitializeComponent();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            //public string useIE_explore = "0";              // 标识是否使用IE浏览器打开
            //public string newDaily_showCosumePannel = "0";  // 是否显示耗时控件逻辑
            //public int minute_Before_TaskDeadline = 10;     // Task任务截止时间前n分钟时，给出将到期提示信息
            //public int second_Refresh_Intervate = 60;       // 任务管理工具获取数据刷新时间间隔

            load();
        }

        // 载入设置信息
        private void load()
        {
            loading = true;

            string str = loginForm.getLocalData("设置");
            if (!str.Equals("")) settings = SettingData.Parse(str);

            showSettings();
            loading = false;

            FormTool.RestoreFormInfo(this);
        }

        // 显示当前的设置信息
        private void showSettings()
        {
            textBox_Intervate.Text = settings.second_Refresh_Intervate + "";
            textBox_TaskDeadline.Text = settings.minute_Before_TaskDeadline + "";
            comboBox_showCosumePannel.SelectedIndex = settings.newDaily_showCosumePannel.Equals("0") ? 0 : 1;
            comboBox_useIE.SelectedIndex = settings.useIE_explore.Equals("0") ? 0 : 1;
            comboBox_FoodRemind_time.Text = settings.FoodRemind_time;

            settings_Changed(null, null);
        }

        // 设置信息变动
        private void settings_Changed(object sender, EventArgs e)
        {
            if (loading) return;

            SettingData settings_tmp = curSetttings();
            String curStr = settings_tmp.ToJson();              // 当前设置
            string usedStr = loginForm.getLocalData("设置");    // 使用中的设置
            String defaultStr = new SettingData().ToJson();     // 默认设置

            // 判断设置是否变动
            button_default.Enabled = !usedStr.Equals(defaultStr);
            button_apply.Enabled = !usedStr.Equals(curStr);
        }

        // 获取当前的设置信息
        private SettingData curSetttings()
        {
            SettingData settings_tmp = new SettingData();

            settings_tmp.second_Refresh_Intervate = Tool.toInt(textBox_Intervate.Text);
            settings_tmp.minute_Before_TaskDeadline = Tool.toInt(textBox_TaskDeadline.Text);
            settings_tmp.newDaily_showCosumePannel = comboBox_showCosumePannel.SelectedIndex + "";
            settings_tmp.useIE_explore = comboBox_useIE.SelectedIndex + "";
            settings_tmp.FoodRemind_time = comboBox_FoodRemind_time.Text;
            if (!settings_tmp.FoodRemind_time.Equals("不提醒")) FoodRemind.remindDate = "";

            return settings_tmp;
        }

        // 使用默认设置
        private void button_default_Click(object sender, EventArgs e)
        {
            useSettings(new SettingData());
        }

        // 应用当前的设置
        private void button_apply_Click(object sender, EventArgs e)
        {
            useSettings(curSetttings());
        }

        // 使用set中设置信息
        private void useSettings(SettingData set)
        {
            settings = set;
            loginForm.settings = settings;
            loginForm.SaveTxt(settings.ToJson(), "设置");
            showSettings();
        }

        private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            FormTool.saveFormInfo(this);        // 保存界面信息
        }

    }
}
